﻿using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Fluid;
using Fluid.Ast;

namespace ZYing.LiquidThemeEngine.Tags
{
    public static class LayoutTags
    {
        public static async ValueTask<Completion> LayoutAsync(Expression expression, TextWriter writer, TextEncoder encoder, TemplateContext ctx)
        {
            var context = (LiquidThemeTemplateContext)ctx;
            var layout = await expression.EvaluateAsync(context);
            context.Layout = layout.ToStringValue();
            return Completion.Normal;
        }
        public static async ValueTask<Completion> RenderBodyAsync(TextWriter writer, TextEncoder encoder, TemplateContext ctx)
        {
            var context = (LiquidThemeTemplateContext)ctx;
            if (context.TryGetBody(out var body)) await writer.WriteAsync(body);
            return Completion.Normal;
        }
        public static async ValueTask<Completion> RenderIndexAsync(TextWriter writer, TextEncoder encoder, TemplateContext ctx)
        {
            var context = (LiquidThemeTemplateContext)ctx;
            if (!context.AmbientValues.TryGetValue(Constants.AmbientNames.IndexContent, out var value))
            {
                value = context.AmbientValues[Constants.AmbientNames.IndexContent] = await BuildIndexContent(context);
            }
            var content = value as string;
            if (!string.IsNullOrWhiteSpace(content)) await writer.WriteLineAsync(content);
            return Completion.Normal;
        }

        private static async ValueTask<string> BuildIndexContent(LiquidThemeTemplateContext context)
        {
            var settings = await context.GetThemeSettingAsync();
            if (settings is { Sections: { }, ContentForIndex: { } })
            {
                var builder = new StringBuilder();
                using (var writer = new StringWriter(builder))
                {
                    foreach (var id in settings.ContentForIndex)
                    {
                        await context.RenderSectionAsync(writer, settings, id, true);
                    }
                }
                return builder.ToString();
            }
            return string.Empty;
        }

        public static async ValueTask<Completion> Solt(string name, TextWriter writer, TextEncoder encoder, TemplateContext ctx)
        {
            var context = (LiquidThemeTemplateContext)ctx;
            if (context.TryGetSoltContent(name, out var content)) await content.RenderStatementsAsync(writer, encoder, context);
            return Completion.Normal;
        }

        public static ValueTask<Completion> Template(string name, IReadOnlyList<Statement> statements, TextWriter writer, TextEncoder encoder, TemplateContext ctx)
        {
            var context = (LiquidThemeTemplateContext)ctx;
            context.SetSlotContent(name, statements);
            return new ValueTask<Completion>(Completion.Normal);
        }
    }
}